package com.zsj.zsjkotlinmvvm.ui.video.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.zsj.zsjkotlinmvvm.data.model.repository.RepositoryData
import com.zsj.zsjkotlinmvvm.data.model.repository.VideoData
import com.zsj.zsjkotlinmvvm.data.repository.GitHubRepository
import com.zsj.zsjkotlinmvvm.data.repository.VideoTitleRepository
import com.zsj.zsjkotlinmvvm.ui.BaseViewModel
import com.zsj.zsjkotlinmvvm.ui.UIState
import javax.inject.Inject

/**
 * Created by TanJiaJun on 2020-02-07.
 */
class VideoTitleViewModel @Inject constructor(
    private val repository: VideoTitleRepository
) : BaseViewModel() {

    private val _isShowRepositoryView = MutableLiveData<Boolean>()
    val isShowRepositoryView: LiveData<Boolean> = _isShowRepositoryView

    private val _repositories = MutableLiveData<List<VideoData>>()
    val repositories: LiveData<List<VideoData>> = _repositories

    fun getRepositories(languageName: String) =
        launch(
            uiState = UIState(isShowLoadingView = true, isShowErrorView = true),
            block = { repository.getVideoList(0, 10) },
            success = {
                Log.e("zsj",it.result.total.toString())
                if (it.result.list.isNotEmpty()) {
                            _repositories.value = it.result.list
                            _isShowRepositoryView.value = true
                }
            }
        )

}